﻿
get_Vehicle_information()
getERPfahuo()
getERPid()
$(document).on("change", "js_cydw", function () {
    get_Vehicle_information()
})
$(document).on("click", "#js_fhdh", function () {
    getERPfahuo()
})
//创建运单
$(document).on("click", "#found", function () {
    found_yd()
})
//提交审核
$(document).on("click", "#tjiao", function () {
    Submit_for_review()
})

//选择拼车
$(document).on("click", "#xunaz", function () {
    Select_Carpool()
})
//删除拼车
$(document).on("click", "#romeve_tr", function (e) {
    $(e.target).parent().parent().remove()
})
function getERPfahuo() {
    var bian = $("#js_fhdh").val()
    if (bian == 0) {
        $.ajax({
            url: '/Freight/Calculate_freight',
            type: 'GET',
            data: { "state": 1},
            success: function (data) {
                createRow_erp(data)
            },
            error: function () {
                alert('Request Error');
            }
        })
    }
    else {
        $.ajax({
            url: '/Freight/Calculate_freight',
            type: 'GET',
            data: { "state": 1, "combs": $("#js_fhdh").val() },
            success: function (data) {
                createRow_erp(data)
            },
            error: function () {
                alert('Request Error');
            }
        })
    }
    
}

function createRow_erp(data) {
    $('#E_tb_e1 tr').remove();
    var index = 1
    $.each(data, function (i, item) {
        if (item.distance == 0) {
            item.distance=50
        }
        if (item.weight == 0) {
            item.weight = 0.0133312
        }
        var $tr = $(`
                                                <tr style="height:20px;line-height:20px;font-size:10px">
                                                    <td>${index}</td>
                                                    <td id="shdh">${item.id}</td>
                                                    <td id="kh">${item.client}</td>
                                                    <td id="dz">${item.address}</td>
                                                    <td id="juli">${item.distance}</td>
                                                    <td class="hw">${item.quantity}</td>
                                                    <td style="display:none">${item.quantity}</td>
                                                    <td style="display:none" class="js_zl">${item.weight}</td>
                                                    <td style="display:none" class="js_tj">${item.volume}</td>
                                                </tr>
                                            `);
        $('#E_tb_e1').append($tr);
        index++
    })
}


function get_Vehicle_information() {
    $.ajax({
        url: '/Freight/Freight_management',
        type: 'GET',
        data: { "state": 1, "id": $("#js_cydw").val() },
        success: function (data) {
            createRow_cl(data)
        },
        error: function () {
            alert('Request Error');
        }
    })


}
//显示车辆信息
function createRow_cl(data) {
    $('#js_clxx tr').remove();
    var index = 1
    $.each(data, function (i, item) {
        if (item.cx_sh2 == "通过") {
            var $tr = $(`
                                                <tr style="height:20px;line-height:20px">
                                                    <td>${index}</td>
                                                    <td id="cph" class="cph">${item.cl_cp}</td>
                                                    <td id="ss" style="display:none">${item.cl_ss}</td>
                                                    <td id="ss_name">${item.name}</td>
                                                    <td id="zz">${item.cl_zz}</td>
                                                    <td id="rj">${item.cl_rj}</td>
                                                </tr>
                                            `);
            $('#js_clxx').append($tr);
            index++
        }
    })
}


function getERPid() {
    $.ajax({
        url: '/Freight/Calculate_freight',
        type: 'GET',
        data: { "state": 2 },
        success: function (data) {
            $("#js_fhdh option").remove()
            $("#js_fhdh").append($(`<option value="0" class="fhdh">===请选择发货单号===</option>`))
            
            $.each(data, function (i, item) {
                var index=0
                $(".fhdh").each(function (o, obj) {
                    if ($(obj).val() == item.id) {
                        index = 1
                        console.log("添加成功")
                    }
                })
                if (index != 1) {
                    $("#js_fhdh").append($(`<option value="${item.id}" class="fhdh">${item.id}${item.client}</option>`))
                }
                
            })
            
        },
        error: function () {
            alert('Request Error');
        }
    })
}

function found_yd() {
    var bian = 0
    var zl = 0
    var tj = 0
    var jg = 0
    var cl = ""
    var minjia = 10000000
    var zz2 = 0
    var rj2 = 0
    var index = 0
    $(".tr_hang").each(function (i, item) {
        bian++
    })
    if (bian == 1) {

        $(".js_zl").each(function (i, item) {
            zl += $(item).text() * 1
        })
        $(".js_tj").each(function (i, item) {
            tj += $(item).text() * 1
        })
        var jl = $("#juli").text()
        $("#js_clxx tr").each(function (i, item) {
            var ss = $("td[id=ss]", item).text()
            var ssname = $("td[id=ss_name]", item).text()
            var cph = $("td[id=cph]", item).text()
            var zz = $("td[id=zz]", item).text() * 1
            var rj = $("td[id=rj]", item).text() * 1
            if (zz > zl && rj > tj) {
                $.ajax({
                    url: '/Freight/Calculate_freight',
                    type: 'GET',
                    data: { "state": 3, "ss": ss, "cph": cph, "gls": jl },
                    success: function (data) {
                        if (minjia > Number(data)) {
                            minjia = data
                            $("#tem1").text(data)
                            $("#tem2").text(ss)
                            $("#tem3").text(cph)
                            $("#tem4").text(ssname)
                            show_yd()
                        }
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
                index = 1
            }

        })

        if (index == 0) {
            console.log(zl, tj)
            var CLSXX = ""
            $(".cph").each(function (i, item) {
                CLSXX += $(item).text() + " "
                console.log(CLSXX)
            })
            
            console.log("需要多车")
            $.ajax({
                url: '/Freight/Calculate_freight',
                type: 'GET',
                data: { "state": 4, "cs":CLSXX, "gls": jl, "zl": zl, "tj": tj },
                success: function (data) {
                    var temp = data.split("/")
                    $("#tem1").text(temp[0])
                    $("#tem5").text(temp[1])
                    show_yd()
                },
                error: function () {
                    alert('Request Error');
                }
            })
        }
        else {
            
        }
    }
    else if (bian > 1)
    {
        console.log(bian)
        var index=0
        var temp1 = 0
        var temp2 = 0

        $(".zl_a1").each(function (i, item) {
            temp1 += $(item).text()*1
        })
        $(".tj_a1").each(function (i, item) {
            temp2 += $(item).text() * 1
        })

        $("#js_clxx tr").each(function (i, item) {
            var zz = $("td[id=zz] ", item).text()*1
            var rj = $("td[id=rj] ", item).text()*1
            var ss = $("td[id=ss]", item).text()
            var ssname = $("td[id=ss_name]", item).text()
            var cph = $("td[id=cph]", item).text()
            if (temp1 < zz && temp2 < rj) {
                index++
                $.ajax({
                    url: '/Freight/Calculate_freight',
                    type: 'GET',
                    data: { "state": 3, "ss": ss, "cph": cph, "gls": jl },
                    success: function (data) {
                        if (minjia > Number(data)) {
                            minjia = data
                            $("#tem1").text(data)
                            $("#tem2").text(ss)
                            $("#tem3").text(cph)
                            $("#tem4").text(ssname)
                            show_yd()
                        }
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

        })
        var min_ss = 0
        var min_cph=""
        var min_zz = 100000
        var zhongj =0
        $("#js_clxx tr").each(function (i, item) {
            if (min_zz > $("td[id=rj] ", item).text() * 1) {
                min_zz = $("td[id=rj] ", item).text() * 1
                min_ss = $("td[id=ss] ", item).text()
                min_cph = $("td[id=cph] ", item).text()
            }
        })

        $("#pingche_td tr").each(function (i, item) {

            var jl = $("th[class=gls_a1]",item).text()
            console.log(jl, min_ss, min_cph)
            $.ajax({
                url: '/Freight/Calculate_freight',
                type: 'GET',
                data: { "state": 3, "ss": min_ss, "cph": min_cph, "gls": jl },
                success: function (data) {
                    zhongj += data * 1
                    console.log(zhongj)
                    $("#jg").text(zhongj)
                },
                error: function () {
                    alert('Request Error');
                }
            })
        })
        
        if (index == 0) {
            alert("请重新配置拼车方案")
        }
    }
    else {
        alert("请先选择发货通知单")
    }
}


function show_yd() {
    var fhdh = ""
    var dz = ""
    var kh = ""
    var wl = ""
    var jl=""
    var jg = $("#tem1").text()
    var ss = $("#tem2").text()
    var cph = $("#tem3").text()
    var name = $("#tem4").text()
    var checi = $("#temp5").text()


    var now = new Date($.now());
    var year = (now.getFullYear()).toString();
    var month = (now.getMonth() + 1).toString();
    var date = (now.getDate()).toString();
    var time = year + "-" + month + "-" + date
    
    $(".hw_a1").each(function (i, item) {
        if (wl == "") {

            wl += $(item).text()
        }
        else {
            wl += "/" + $(item).text()
        }
    })

    $(".fhdh_a1").each(function (i, item) {
        if (fhdh == "") {
            fhdh += $(item).text()
        }
        else {
            fhdh += "/" + $(item).text()
        }
    })
    $(".gls_a1").each(function (i, item) {
        if (jl == "") {
            jl += $(item).text()
        }
        else {
            jl += "/" + $(item).text()
        }
    })
    $(".diz_a1").each(function (i, item) {
        if (dz == "") {
            dz += $(item).text()
        }
        else {
            dz += "/" + $(item).text()
        }
    })
    $(".kh_a1").each(function (i, item) {
        if (kh == "") {
            kh += $(item).text()
        }
        else {
            kh += "/" + $(item).text()
        }
    })

    $("#yundan_xx tr").remove()
    $("#yundan_xx").append(
        $(`
                    <tr>
                            <td style="display:none" id="ss">${ss}</th>
                            <td>1</th>
                            <td id="fhdh">${fhdh}</th>
                            <td id="kh">${kh}</th>
                            <td id="dz">${dz}</th>
                            <td id="wl">${wl}</th>
                            <td id="cjrq">${time}</th>
                            <td id="shdh"></th>
                            <td>${name}</th>
                            <td id="cc">${checi}</th>
                            <td id="jl">${jl}</th>
                            <td id="jg">${jg}</th>
                            <td id="cph">${cph}</th>
                        </tr>
        `)
    )
    $("#temp5").text("1")
}

function Submit_for_review() {
    var BOMinise = [];
    $("#yundan_xx tr").each(function (i, obj) {
        BOMinise.push({
            yd_fhdh: $("td[id=fhdh] ", obj).text(),//
            yd_kh: $("td[id=kh] ", obj).text(),//
            yd_shdz: $("td[id=dz] ", obj).text(),//
            yd_hw: $("td[id=wl] ", obj).text(),//
            yd_cjrq: $("td[id=cjrq] ", obj).text(),//
            yd_shdh: $("td[id=shdh]", obj).text(), //
            yd_cydw: $("td[id=ss] ", obj).text(),//
            yd_cc: $("td[id=cc]", obj).text(),
            yd_gls: $("td[id=jl] ", obj).text(),
            yd_yf: $("td[id=jg] ", obj).text(),
            yd_cph: $("td[id=cph] ", obj).text(),
            //预留添加标识
        })
        console.log("共" + BOMinise.length + "数据");
        console.log(BOMinise);
    })

    $.ajax({
        url: "/Freight/ADD_YD",
        type: "POST",
        data: JSON.stringify(BOMinise),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (text) {
            alert(text)
        },
        error: function (text) {
            alert("系统错误" + text);
        }
    })
}

function Select_Carpool() {
    var index = $("#js_fhdh").val()
    if (index == "0") {
        alert("请选择发货单")
    }
    else {
        
        var fhdh = $("#shdh").text()
        var kh = $("#kh").text()
        var gls = $("#juli").text()
        var diz = $("#dz").text()
        var hw = ""
        var zl = 0
        var tj = 0
        index = 1
        var temp_add_yd=0
        $(".hw").each(function (i, item) {
            if (hw = "") {
                hw += $(item).text()
            }
            else {
                hw += "/"+$(item).text()
            }
        })
        $(".js_zl").each(function (i, item) {
            zl += $(item).text()*1
        })

        $(".js_tj").each(function (i, item) {
            tj += $(item).text() * 1
        })
        $(".tr_hang").each(function (i, item) {
            index++
        })
        var $tr = 
            $(`
                    <tr class="tr_hang">
                        <th>${index}</th>
                        <th class="fhdh_a1">${fhdh}</th >
                        <th class="gls_a1">${gls}</th>
                        <th class="zl_a1">${zl}</th>
                        <th class="tj_a1">${tj}</th>
                        <th><a href="#" id="romeve_tr">删除</a></th>
                        <th style="display:none" class="hw_a1">${hw}</th>
                        <th style="display:none" class="diz_a1">${diz}</th>
                        <th style="display:none" class="kh_a1">${kh}</th>
                    </tr>
             `)
        $(".tr_hang").each(function (i, item) {
            if (fhdh == $("th[class=fhdh_a1]", item).text()) {
                
                temp_add_yd++
            }
        })
        console.log(temp_add_yd)
        if (temp_add_yd == 0) {
            $("#pingche_td").append($tr)
        }
        else {
            alert("请勿重复提交")
        }
        
    } 
}

